﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
'Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports X360.IO
Imports X360.STFS
Imports X360.Security.Cryptography
Imports X360.Profile
Imports System.IO



Namespace Mod_App
    Module resigner


        Private pack As STFSPackage
        Private entry As FileEntry
        Private savedata As DJsIO

        ' NOTE: The X360 Library is protected under a specific Terms and Service and License
        '
        ' Make sure you read that before doing anything, mainly because it REQUIRES you
        '
        ' To make everything open source and NOT make profit off it

        Friend Sub OpenResign(ByVal sender As Object, ByVal e As EventArgs)
            Dim xPackage As New STFSPackage(sender.ToString, Nothing) 'sender)
            If Not xPackage.ParseSuccess Then
                xPackage.CloseIO()
                Exit Sub
            Else
                If File.Exists(Application.StartupPath & "\Resources\KV.bin") Then
                    xPackage.FlushPackage(New RSAParams(Application.StartupPath & "\Resources\KV.bin"))
                    xPackage.CloseIO()
                    Main.LogListBox.Items.Add("   Resigned " & Main.SavesListView.Items(Main.counter).Text)
                    Main.LogListBox.SelectedIndex = Main.LogListBox.Items.Count - 1
                    Main.LogListBox.SelectedIndex = -1
                Else
                    Main.LogListBox.Items.Add("   KV.bin is missing, cannot resign")
                    Main.LogListBox.SelectedIndex = Main.LogListBox.Items.Count - 1
                    Main.LogListBox.SelectedIndex = -1
                End If
            End If
            '    ' Note: to access a file, make a path, such as "assets/saves/save1"
            '    ' or if its in the root, just "save"
            '    Dim xent As FileEntry = xPackage.GetFile("C:\Users\Scuba156\Desktop\Xbox360\Apps\Le Fluffie App\KV.bin")
            '    If xent Is Nothing Then
            '        ' if the library wasnt able to find the file
            '        xPackage.CloseIO()
            '        MessageBox.Show("Error: could not locate save data")
            '        Exit Sub
            '    End If
            '    ' Set up temporary save extraction file and extract it
            '    Dim outlocale As String = X360.Other.VariousFunctions.GetTempFileLocale()
            '    If Not xent.Extract(outlocale) Then
            '        ' if the library wasnt able to extract it
            '        xPackage.CloseIO()
            '        MessageBox.Show("Error: could not locate save data")
            '        Exit Sub
            '    End If
            '    If pack IsNot Nothing Then
            '        pack.CloseIO()
            '    End If
            '    ' close previous IO (if applicable)
            '    If savedata IsNot Nothing Then
            '        savedata.Close()
            '    End If
            '    ' close the previous IO (if applicable)
            '    ' Set global variables and enable buttons
            '    savedata = New DJsIO(outlocale, DJFileMode.Open, True)
            '    entry = xent
            '    pack = xPackage
            '    'textBox1.Text = xPackage.FileNameLong
            '    'button1.Enabled = InlineAssignHelper(button2.Enabled, True)
            'End Sub

            'Friend Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
            '    ' Mod code goes here
            '    ' Notes:
            '    ' Use savedata as the IO to read and write from
            '    ' Use offsets relative to the extracted file, not the package
            'End Sub

            'Friend Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            '    If Not entry.Replace(savedata) Then
            '        ' If the library was unable to replace the file
            '        MessageBox.Show("Error: modifications were not saved")
            '        Exit Sub
            '    End If
            '    If Not pack.FlushPackage(New RSAParams(Application.StartupPath & "/KV.bin")) Then
            '        ' If the library was unable to fix the file
            '        MessageBox.Show("Error: could not hash/sign the save")
            '        Exit Sub
            '    End If
            '    MessageBox.Show("Done")
            'End Sub
            'Friend Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
            '    target = value
            '    Return value
        End Sub
    End Module
End Namespace
